package com.admin.service.impl;

import com.admin.common.enums.MemberLevelEnum;
import com.admin.domain.Declare;
import com.admin.domain.Region;
import com.admin.mapper.RegionMapper;
import com.admin.service.RegionsService;
import com.admin.vo.ResultVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 业务接口实现类
 *
 * @author yangzhongliang
 *
 */
@Service
public class RegionsServiceImpl implements RegionsService {

    @Resource
    private RegionMapper regionMapper;

    @Override
    public String findChildren(String parentIds) {
        StringBuffer sb = new StringBuffer();
        List<Integer> result = new ArrayList<>();
        List<Integer> parentIdsArray = new ArrayList<>();
        String[] arrayStr = parentIds.split(",");
        for (String s : arrayStr) {
            if (StringUtils.isEmpty(s)) {
                continue;
            }
            parentIdsArray.add(Integer.parseInt(s));
        }
        if (CollectionUtils.isEmpty(parentIdsArray)) {
            return null;
        }
        List<Integer> newList = new ArrayList<>();
        newList.add(parentIdsArray.get(parentIdsArray.size() - 1));
        result.addAll(regionMapper.findChildren(newList));
        if (CollectionUtils.isEmpty(result)) {
            return null;
        }
        for (Integer integer : result) {
            sb.append(integer).append(",");
        }
        return sb.toString();
    }

    @Override
    public List<Integer> findChildrens(String parentIds) {
        List<Integer> parentIdsArray = new ArrayList<>();
        String[] arrayStr = parentIds.split(",");
        for (String s : arrayStr) {
            if (StringUtils.isEmpty(s)) {
                continue;
            }
            parentIdsArray.add(Integer.parseInt(s));
        }
        return regionMapper.findChildren(parentIdsArray);
    }

    @Override
    public ResultVO<List<Region>> getAllCountry(Region regions) {
        ResultVO resultVO = new ResultVO();
        resultVO.setCode(0);
        List<Region> allCountry = regionMapper.getAllCountry(regions);
        resultVO.setData(allCountry);
        return resultVO;
    }


    @Override
    public String getNameByIds(String ids) {
        if (StringUtils.isEmpty(ids)) {
            return null;
        }
        List<Integer> regionIds = new ArrayList<>();
        String[] idArrays = ids.split(",");
        for (String idArray : idArrays) {
            if (StringUtils.isEmpty(idArray)) {
                continue;
            }
            regionIds.add(Integer.parseInt(idArray));
        }
        List<Region> regions = regionMapper.findByIds(regionIds);
        if (CollectionUtils.isEmpty(regions)) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        for (Region region : regions) {
            sb.append(region.getRegionName()).append(",");
        }
        return sb.toString();
    }

    @Override
    public String getById(Integer id) {
        Region region = regionMapper.selectByPrimaryKey(id);
        if (Objects.isNull(region)) {
            return "";
        }
        return region.getRegionName();
    }

    @Override
    public String getDeclareRegionName(Declare declare, String userLevel) {
        Integer regionId = 0;
        String userLevelName = null;
        if (StringUtils.isEmpty(userLevel)) {
            return "";
        }
        if (userLevel.equals(MemberLevelEnum.AREA.getKey())) {
            regionId = declare.getAreaId();
        } else if (userLevel.equals(MemberLevelEnum.CITY.getKey())) {
            regionId = declare.getCityId();
        } else if (userLevel.equals(MemberLevelEnum.PROVINCE.getKey())) {
            regionId = declare.getProvinceId();
        }
        Region region = regionMapper.selectByPrimaryKey(regionId);
        if (!Objects.isNull(region)) {
            userLevelName = region.getRegionName();
        }
        return userLevelName;
    }

    @Override
    public Region getByRegionId(Integer regionId) {
        return regionMapper.selectByPrimaryKey(regionId);
    }
}

